package com.study.aspect;

import com.study.util.BeanUtil;
import com.study.util.Page;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collection;

/**
 * @author: chenhs
 * @date: Created in 6:41 2019/11/28
 **/
@Component
@Aspect
public class SetFieldValueAspect {

    @Autowired
    private BeanUtil beanUtil;

    @Around("@annotation(com.study.annotation.NeedSetFieldValue)")
    public Object doSetFieldValue(ProceedingJoinPoint pjp) throws Throwable {
        //不作前置处理，拿到结果集
        Object ret = pjp.proceed();

        /**
         * {id=1, customerId, customerName=xxx}
         * {id=1, customerId, customerName=xxx}
         * {id=1, customerId, customerName=xxx} @NeedSetValue
         */
        if (ret instanceof Page) {
            beanUtil.setFieldValue4Collection(((Page) ret).getItems());
        }

        return ret;
    }


}
